package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbMaterialFile;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbMaterialFileDao {

    @Results(id = "pcbMaterialFileResultMap", value = {
            @Result(column = "pcb_material_file_id", property = "pcbMaterialFileId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_material_factory_id", property = "pcbMaterialFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "factory_id", property = "factoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "row_key", property = "rowKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "file_name", property = "fileName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "file_extend", property = "fileExtend", jdbcType = JdbcType.VARCHAR),
            @Result(column = "file_relate_path", property = "fileRelatePath", jdbcType = JdbcType.VARCHAR),
            @Result(column = "file_type", property = "fileType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "url", property = "url", jdbcType = JdbcType.VARCHAR),
            @Result(column = "file_status", property = "fileStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_MATERIAL_FILE +
            " where 1 = 1 " +
            "<if test='id != null'>" +
            " and pcb_material_factory_id = #{pcbFactoryMaterialId}" +
            "</if>" +
            "<if test='fileType != null'>" +
            " and file_type = #{fileType} " +
            "</if>" +
            " order by sort" +
            "</script>")
    List<PcbMaterialFile> getAllByFactoryMaterialId(@Param("id") Integer pcbFactoryMaterialId, @Param("fileType") Integer fileType);

    /*
     * 单个查询
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_MATERIAL_FILE +
            " where 1 = 1 " +
            "<if test='id != null'>" +
            " and pcb_material_file_id = #{id}" +
            "</if>" +
            " order by sort" +
            "</script>")
    PcbMaterialFile getAllById(@Param("id") Integer id);

    /*
     * 单个查询
     */
    @Select("<script> " +
            "select pcb_material_file_id,pcb_factory_id, pcb_material_factory_id, row_key, file_name, file_extend, file_relate_path, file_type, url, file_status, remark, create_time, create_user, update_time, update_user, status, sort" +
            " from " + DB2TableName.PCB_MATERIAL_FILE +
            " where 1 = 1 " +
            "<if test='rowKey != null'>" +
            " and row_key = #{rowKey} and pcb_factory_id = #{factoryId}" +
            "</if>" +
            " order by sort" +
            "</script>")
    @ResultMap(value = "pcbMaterialFileResultMap")
    List<PcbMaterialFile> getAllByRowKey(@Param("rowKey") String rowKey, @Param("factoryId") Integer factoryId);

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_MATERIAL_FILE + " (pcb_factory_id, pcb_material_factory_id, row_key, file_name, file_extend, file_relate_path, file_type, url, file_status, remark, create_time, create_user, update_time, update_user, status, sort) values " +
            "(#{pcbFactoryId}, #{pcbMaterialFactoryId}, #{rowKey}, #{fileName}, #{fileExtend}, #{fileRelatePath}, #{fileType}, #{url}, #{fileStatus}, #{remark}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    // 显式设置主键
    @Options(useGeneratedKeys = true, keyProperty = "pcbMaterialFileId")
    Integer insert(PcbMaterialFile param);

    /*
     *通过主键删除
     */
    @Delete("<script>" +
            "delete from " + DB2TableName.PCB_MATERIAL_FILE +
            " where pcb_material_factory_id = #{pcbMaterialFactoryId} " +
            "<if test='fileType != null'>" +
            " and file_type = #{fileType} " +
            "</if>" +
            "</script>")
    Integer deleteByRowKey(@Param("pcbMaterialFactoryId") Integer pcbMaterialFactoryId, @Param("fileType") Integer fileType);
}
